Skip to content

Parse escaped quotes in OpenMetrics exemplar label values - #1230

Merged
csmarchbanks merged 1 commit into
prometheus:masterfrom
spicysharkco:fix-om-exemplar-escaped-quote
Oct 30, 2026
Merged

csmarchbanks merged 1 commit into
prometheus:masterfrom
spicysharkco:fix-om-exemplar-escaped-quote

Conversation

@spicysharkco

Copy link
Copy Markdown
Contributor

_parse_remaining_text in openmetrics/parser.py flips its in-quotes state on every ", escaped or not. An exemplar label value that contains a quote, which openmetrics.exposition.generate_latest writes as \", never reaches the closing } and the line is rejected, so the parser cannot read the writer's own output:

>>> list(text_string_to_metric_families('# TYPE a counter\n# HELP a help\na_total 1 # {foo="x\\"y"} 1\n# EOF\n'))
ValueError: Invalid line: # {foo="x\"y"} 1

The scan now skips a quote that is preceded by an odd number of backslashes, using the _is_character_escaped helper already in the module. The same applies to histogram and gaugehistogram bucket exemplars, which go through the same function.

Added test_exemplar_label_escaping next to test_label_escaping. It fails on master with the error above and passes with the change; the \\ row guards the closing quote after an escaped backslash. tests/openmetrics passes, flake8 and isort are clean. No issue exists for this.

@csmarchbanks

The exemplar scanner in _parse_remaining_text flipped its in-quotes
state on every double quote, so a label value like x"y, which the
writer emits as x\"y, made the whole line fail with "Invalid line".
Ignore quotes preceded by an odd number of backslashes.

Signed-off-by: spicysharkco <321699413+spicysharkco@users.noreply.github.com>

@csmarchbanks csmarchbanks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@csmarchbanks
csmarchbanks merged commit ab6be20 into prometheus:master Oct 30, 2026
2 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants